package jp.melodroid.no6.data_picker;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import java.util.Calendar;
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.widget.DatePicker;


public class HelloDataPicker extends Activity
{
    private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

	mDateDisplay = (TextView)findViewById(R.id.dateDisplay);
	mPickDate = (Button)findViewById(R.id.pickDate);

	mPickDate.setOnClickListener( new View.OnClickListener() {
		public void onClick(View v) {
		    showDialog(DATE_DIALOG);
		}
	    });

	final Calendar c = Calendar.getInstance();
	mYear = c.get(Calendar.YEAR);
	mMonth = c.get(Calendar.MONTH);
	mDay = c.get(Calendar.DAY_OF_MONTH);

	updateDisplay();
    }

    private void updateDisplay() {
	mDateDisplay.setText(
			     new StringBuilder().append(mMonth + 1).
			     append("-").
			     append(mDay).append("-").
			     append(mYear).append(" "));
    }

    //for dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener = 
	new DatePickerDialog.OnDateSetListener() {
	    public void onDateSet(DatePicker view , int year,
				  int month, int day) {
		mYear = year;
		mMonth = month;
		mDay = day;

		updateDisplay();
	    }
	};

    protected Dialog onCreateDialog(int id) {
	switch (id) {
	case DATE_DIALOG:
	    return new DatePickerDialog(this,
					mDateSetListener,
					mYear,mMonth,mDay);
	default:
	    break;
	}
	return null;
    }

}
